home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / amos / jwindows.lha / ListViewc.asc < prev    next >
Text File  |  1996-04-25  |  8KB  |  211 lines

  1. '******************************************************************
  2. '* 
  3. '*   ListView
  4. '* 
  5. '******************************************************************
  6.  
  7. '   This example demonstrates the use of a simple listview gadget and
  8. 'how to manipulate a list. The controls allow you to add, move, change and 
  9. 'delete items from the list to your hearts content. The routine is fairly
  10. 'useful in many programs so nick it if you want. 
  11.  
  12. '   All the work for the list is done in the _HANDLEGADGETS procedure, and 
  13. 'no changes whatsoever have been made to the GadToolsBox generated procs.
  14. '_HANDLEGADGETS makes any changes required to the list, then updates all 
  15. 'the gadgets, disabling or enabling buttons as required. 
  16. '   Working with lists can be slightly hazardous if you're not careful,
  17. 'so nodes are ALWAYS checked before they are changed. This isn't strictly
  18. 'necessary since I don't think there are bugs in the code, but I'm not 
  19. 'taking chances... 
  20.  
  21. '******************************************************************
  22.  
  23. Global _SCRAPTAGS,SCRAPTAGS,_PORTLIST,_MESSLIST
  24. Global PATH$,OSVER
  25. Global FHEIGHT,FWIDTH,MBAR,OX,OY,SW,SH
  26. Dim _LISTVIEWLISTS(1)
  27. Global _LISTVIEWLISTS()
  28. Dim _LISTVIEWGADS(6)
  29. Global _LISTVIEWGADS()
  30. Global _LISTSTRING,_LISTVIEW,_UP,_DOWN,_ADD,_DEL
  31. Dim _LISTVIEWZOOM(1)
  32. Global _LISTVIEWZOOM()
  33. Global _LISTVIEWWIND
  34.  
  35. '** This variable tracks the presently selected item in the listview 
  36. '   Items in the listview are numbered 0 to numitems-1, or -1 if no item 
  37. '   is selected. It starts as -1 since there are initially no items in the 
  38. '   list, so obviously no item is selected.
  39. '   Note that commands like J Find Node number the nodes 1 to numitems, so 
  40. '   we usually add one to this value before use. 
  41. Global _LISTSEL
  42. _LISTSEL=-1
  43.  
  44. On Error Proc _CLEANUP
  45. _INITIALIZE
  46. _GUIDATA
  47. _SETUPALL
  48. _SETPORTS
  49. Do 
  50.    K=J Wait Message
  51.    While K
  52.       C=J Tag Data(_MESSLIST,1)
  53.       If C=Equ("IDCMP_CLOSEWINDOW")
  54.          _CLEANUP
  55.       Else If C=Equ("IDCMP_REFRESHWINDOW")
  56.          _DOREFRESH
  57.       Else If C=Equ("IDCMP_GADGETUP")
  58.          _HANDLEGADGETS
  59.       End If 
  60.       K=J Next Message
  61.    Wend 
  62. Loop 
  63.  
  64. Procedure _HANDLEGADGETS
  65.    On Error Proc _CLEANUP
  66.    
  67.    'G is the gadget selected, C is the code field of the message. 
  68.    'This is done purely to give me less typing. 
  69.    G=J Tag Data(_MESSLIST,4)
  70.    C=J Tag Data(_MESSLIST,2)
  71.    
  72.    'First, we deal with the gadgets...
  73.  
  74.    If G=_LISTVIEWGADS(_ADD)
  75.       'The add button adds a node to the list. If no item is presently 
  76.       'selected, it is added to the end of the list using J Make Node. 
  77.       'If an item is selected, we find the present node using J Find Node, 
  78.       'then insert the new node after this using J Insert Node.
  79.       'Note that all the strings are made with length 30. This allows us 
  80.       'to overwrite a longer string when the name is changed without making
  81.       'a new string. The maximum length of strings in the string gadget has
  82.       'been set to 30 so we never over-run this limit. 
  83.       If _LISTSEL=-1
  84.          N=J Make Node(_LISTVIEWLISTS(0),J Make String("New Item",30),0)
  85.       Else 
  86.          N=J Find Node(_LISTVIEWLISTS(0),_LISTSEL+1)
  87.          If N
  88.             N=J Insert Node(_LISTVIEWLISTS(0),N,J Make String("New Item",30),0)
  89.          End If 
  90.       End If 
  91.  
  92.    Else If G=_LISTVIEWGADS(_LISTVIEW)
  93.       'If the user has selected a new item by clicking on the listview, we 
  94.       'change the value of _LISTSEL. 
  95.       _LISTSEL=C
  96.  
  97.    Else If G=_LISTVIEWGADS(_DEL)
  98.       'The Del button removes a node. First, we find the presently selected
  99.       'node, and check this exists (in case something has gone wrong..). 
  100.       'The node is then removed using J Remove Node. We then need to adjust  
  101.       '_LISTSEL. Users writing for V39+ could use J Get Gadget Data to do
  102.       'this more easily. 
  103.       If _LISTSEL<>-1
  104.          N=J Find Node(_LISTVIEWLISTS(0),_LISTSEL+1)
  105.          If N
  106.             N=J Remove Node(N)
  107.             If _LISTSEL+1>J Count Nodes(_LISTVIEWLISTS(0))
  108.                Dec _LISTSEL
  109.             End If 
  110.          End If 
  111.       End If 
  112.  
  113.    Else If G=_LISTVIEWGADS(_LISTSTRING)
  114.       'The string gadget allows strings to be changed or entered. If a string  
  115.       'is entered when no item is selected, we make this into a new node.
  116.       'Otherwise, we change the string for the presently selected item.
  117.       'This is done using J Change String. Note that the string pointer
  118.       'returned from J Gadget String points to a READ ONLY string which we 
  119.       'copy. 
  120.       If _LISTSEL<>-1
  121.          N=J Find Node(_LISTVIEWLISTS(0),_LISTSEL+1)
  122.          If N
  123.             J Change String J Node Name(N),J Read String$(J Gadget String(_LISTVIEWGADS(_LISTSTRING)))
  124.          End If 
  125.       Else 
  126.          N=J Make Node(_LISTVIEWLISTS(0),J Make String(J Read String$(J Gadget String(_LISTVIEWGADS(_LISTSTRING))),30),0)
  127.       End If 
  128.  
  129.    Else If G=_LISTVIEWGADS(_UP)
  130.       'To move an item upwards, we first find the present node, then the node
  131.       '2 nodes before - this is the node we will insert it after. For example, 
  132.       'moving node 3 up, it becomes node 2, and should thus be inserted
  133.       'AFTER node 1. Weird, but that's how J Move Node works.
  134.       'We then adjust _LISTSEL to keep the moving node selected. 
  135.       N=J Find Node(_LISTVIEWLISTS(0),_LISTSEL+1)
  136.       If N
  137.          M=J Prev Node(N)
  138.          M=J Prev Node(M)
  139.          J Move Node _LISTVIEWLISTS(0),N,M
  140.          Dec _LISTSEL
  141.       End If 
  142.  
  143.    Else If G=_LISTVIEWGADS(_DOWN)
  144.       'To move an item down, we insert it after the next node. Very like 
  145.       'the above...
  146.       N=J Find Node(_LISTVIEWLISTS(0),_LISTSEL+1)
  147.       If N
  148.          M=J Next Node(N)
  149.          J Move Node _LISTVIEWLISTS(0),N,M
  150.          Inc _LISTSEL
  151.       End If 
  152.    End If 
  153.    
  154.    'Since every routine above causes some change, we should update all
  155.    'the gadgets now...
  156.  
  157.    'First we update the ListView. If any part of the List being displayed 
  158.    'is changed, we must send a GTLV_Labels tag, or the change doesn't show
  159.    'up. Also, because the Up and Down gadgets change the _LISTSEL variable, 
  160.    'we must change the selected item on the list with the GTLV_Selcted tag. 
  161.    J Tag SCRAPTAGS,1,Equ("GTLV_Labels"),_LISTVIEWLISTS(0)
  162.    J Tag Equ("GTLV_Selected"),_LISTSEL
  163.    J Tag 0,0
  164.    J Set Gadget Data _LISTVIEWGADS(_LISTVIEW),_LISTVIEWWIND,SCRAPTAGS
  165.  
  166.    'Now for the Del button. As long as an item is selected we can delete it,
  167.    'but if not then the button should be disabled. Note the double check
  168.    'using J List Not Empty. 
  169.    If J List Not Empty(_LISTVIEWLISTS(0)) and _LISTSEL<>-1
  170.       J Tag SCRAPTAGS,1,Equ("GA_Disabled"),False
  171.    Else 
  172.       J Tag SCRAPTAGS,1,Equ("GA_Disabled"),True
  173.    End If 
  174.    J Tag 0,0
  175.    J Set Gadget Data _LISTVIEWGADS(_DEL),_LISTVIEWWIND,SCRAPTAGS
  176.  
  177.    'As long as the selected item is the 2nd node or higher, the Up button is
  178.    'enabled. Note that a J Tag 0,0 line is not required here since it's still 
  179.    'present from changing the last button.
  180.    If _LISTSEL>0
  181.       J Tag SCRAPTAGS,1,Equ("GA_Disabled"),False
  182.    Else 
  183.       J Tag SCRAPTAGS,1,Equ("GA_Disabled"),True
  184.    End If 
  185.    J Set Gadget Data _LISTVIEWGADS(_UP),_LISTVIEWWIND,SCRAPTAGS
  186.  
  187.    'If an item is selected, and it isn't the last item in the list, the 
  188.    'Down button is enabled. 
  189.    If _LISTSEL+1<J Count Nodes(_LISTVIEWLISTS(0)) and _LISTSEL<>-1
  190.       J Tag SCRAPTAGS,1,Equ("GA_Disabled"),False
  191.    Else 
  192.       J Tag SCRAPTAGS,1,Equ("GA_Disabled"),True
  193.    End If 
  194.    J Set Gadget Data _LISTVIEWGADS(_DOWN),_LISTVIEWWIND,SCRAPTAGS
  195.    
  196.    'The Add button is always enabled, so we can ignore this.
  197.  
  198. End Proc
  199.  
  200. 'Gadstools created procs 
  201.  
  202. Procedure _INITIALIZE
  203. Procedure _SETUPALL
  204. Procedure _GUIDATA
  205. Procedure _MAKELISTVIEWGADS
  206. Procedure _MAKELISTVIEWWIND[SC]
  207. Procedure _DOREFRESH
  208. Procedure _SETPORTS
  209. Procedure _FREEWIND[W,G,M,A,C]
  210. Procedure _CLEANUP
  211.